//获取章节信息

import axios from "axios";
import { ElMessage } from "element-plus";
import { ref } from "vue";
import { getCurrentInstance } from "vue";

export async function addAnnotationApi(book_id, chapter_id, start_pos, end_pos, content, type, proxy) {
  console.log( book_id, chapter_id, start_pos, end_pos, content, type,proxy);
  const formData = new FormData();
  formData.append("book_id", book_id);
  formData.append("chapter_id", chapter_id);
  formData.append("start_pos", start_pos);
  formData.append("end_pos", end_pos);
  formData.append("content", String(content.value));
  formData.append("type", String(type.value));
  try {
    const response = await proxy.$http.post(
      "/api/book/add-annotation",
      formData,
      {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      }
    );
    const res = response.data.data;
    console.log(res);
    const isSuccess = response.data.success;
    const message = response.data.message;
    // console.log(response.data);
    if (isSuccess === false) {
      // ElMessage.error(message);
    } else {
      // ElMessage.success("获取成功");
      return true;
    }
  } catch (error) {
    console.error(`Error fetching annotation:`, error);
    return [];
  }
}

export async function deleteAnnotationApi(annotationId,proxy) {
  console.log(annotationId);
  try {
    const response = await proxy.$http.post("/api/book/delete-annotation", {
      annotation_id: annotationId,
    });
    const isSuccess = response.data.success;
    const message = response.data.message;
    if (isSuccess === false) {
      // ElMessage.error(message);
    } else {
      ElMessage.success("删除批注成功");
      return true;
    }
  } catch (error) {
    console.error(`Error delete ${highlightId} hightlight:`, error);
    return [];
  }
}
